/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.common.output.writer;

import java.io.IOException;
import java.io.Writer;

import org.telosys.common.TelosysRuntimeException;
import org.telosys.common.dataset.DataRow;
import org.telosys.common.dataset.DataSet;
import org.telosys.util.JsonUtil;

/**
 * Utility class providing methods to write a Dataset in JSON format <br>
 * <br>
 * To use an OutputStream : Writer writer = new OutputStreamWriter(outputStream);
 * 
 * @author Laurent GUERIN
 *  
 */
public class DataSetJsonWriter //extends TelosysObject
{
    public  final static int  DATA_ROW_AS_ARRAY  = 1 ;
    public  final static int  DATA_ROW_AS_OBJECT = 2 ;
    
    private final static String END_OF_LINE = "\n" ;
    
    //-----------------------------------------------------------------------------------
    /**
     * Writes the DataSet in JSON 
     * @param writer
     * @param dataSet
     */
    public static void toJson(Writer writer, DataSet dataSet )
    {
    	toJson(writer, dataSet, DATA_ROW_AS_ARRAY);
    }
    
    //-----------------------------------------------------------------------------------
//    //-----------------------------------------------------------------------------------
//    /**
//     * Writes the DataSet in JSON 
//     * @param writer
//     * @param ds
//     * @param sDataSetTagName the name of tag for the DataSet
//     */
//    public static void toJSON(Writer writer, DataSet ds, String sDataSetTagName)
//    {
//    	toJSON(writer, ds, sDataSetTagName, null);
//    }
    //-----------------------------------------------------------------------------------
    /**
     * Writes the DataSet in JSON as an array of DataRow
     * @param writer
     * @param dataSet
     * @param dataRowOutputType how to represent each DataRow : as an ARRAY or as an OBJECT 
     */
    public static void toJson(Writer writer, DataSet dataSet, int dataRowOutputType)
    {
        if ( writer == null )
        {
            throw new IllegalArgumentException("Writer is null");
        }
        if (dataSet == null)
        {
            throw new IllegalArgumentException("DataSet is null");
        }
        
        try
        {
            writer.write("[ ");
            writer.write(END_OF_LINE);
            
            int iRowCount = dataSet.getRowCount();
            DataRow dataRow = null;
            for ( int iRow = 1 ; iRow <= iRowCount ; iRow++ )
            {
            	if ( iRow > 1 ) {
                    writer.write("," );
                    writer.write(END_OF_LINE);
            	}
                dataRow = dataSet.getDataRow(iRow);
                if (dataRow != null)
                {
                	if ( dataRowOutputType == DATA_ROW_AS_ARRAY ) {
                    	writeDataRowAsArray( writer, dataRow );
                	}
                	else {
                    	writeDataRowAsObject( writer, dataRow );
                	}
                }
                else {
                    writer.write("null" ); // Never happens
                }
                writer.write(END_OF_LINE);
            }
            writer.write("] " );
            writer.write(END_OF_LINE);
            
            writer.flush();
            
        } catch (IOException ex)
        {
            throw new TelosysRuntimeException("toJSON : IOException", ex);
        }
    }
    //-----------------------------------------------------------------------------------
    private static void writeDataRowAsArray(Writer writer, DataRow dataRow ) throws IOException
    {
        if (dataRow != null)
        {
            int iSize = dataRow.getSize();
            writer.write(" [ " );
            //--- For each column in the DataRow
            for ( int iCol = 1 ; iCol <= iSize ; iCol++ )
            {
            	if ( iCol > 1 ) {
                    writer.write(", ");
            	}
                Object obj = dataRow.getObject(iCol);
                writer.write ( JsonUtil.jsonObjectValue(obj) );
            }
            writer.write(" ] ");
        }
    }
    //-----------------------------------------------------------------------------------
    private static void writeDataRowAsObject(Writer writer, DataRow dataRow ) throws IOException
    {
        if (dataRow != null)
        {
            int iSize = dataRow.getSize();
            writer.write(" { " );
            //--- For each column in the DataRow
            for ( int iCol = 1 ; iCol <= iSize ; iCol++ )
            {
            	if ( iCol > 1 ) {
                    writer.write(", ");
            	}
            	Object obj = dataRow.getObject(iCol);
                writer.write(" \"c" + iCol + "\" : " + JsonUtil.jsonObjectValue(obj) );
            }
            writer.write(" } ");
        }
    }
    //-----------------------------------------------------------------------------------
}